﻿using DevExpress.XtraEditors;
using RecruitStudents.Client.BLL;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Const;
using RecruitStudents.Utility.Helper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Appointment
{
    public partial class Appointment_MakeAppointmentForm_Second_List : BaseForm
    {
        ServiceAppointment service = new ServiceAppointment();
        ServiceComm bllcommService = new ServiceComm();
      
        public Appointment_MakeAppointmentForm_Second_List()
        {
            InitializeComponent();
            ClearTextBox();
            this.txtIdCard.KeyPress += txtIdCard_KeyPress;
            AddControl();
        }

        private void AddControl()
        {
            string[] strSpecialityKindsNO = SessionerHelper.SpecialityKindsNO.ToSafeString().Split(',');
            string[] strSpecialityKindsName = SessionerHelper.SpecialityKinds.ToSafeString().Split(',');
            this.xtraScrollableControl1.Controls.Clear();
            //ExamSpecialityKinds examRoom = null;
            //for (int i = 0; i < strSpecialityKindsNO.Length; i++)
            //{
            //    examRoom = new ExamSpecialityKinds(strSpecialityKindsNO[i], strSpecialityKindsName[i]);
            //    examRoom.Location = CalculatePoint(i, strSpecialityKindsNO.Length);
            //    examRoom.GroupNO = GetGourpIDBySK(strSpecialityKindsNO[i]);
            //    examRoom.InitAppointmentStudents();
            //    this.xtraScrollableControl1.Controls.Add(examRoom);
            //}
        }

        private Point CalculatePoint(int sort, int columNum)
        {
            const int examRoomWidth = 270;
            const int examRoomHeight = 390;
            if (columNum == 0)
                columNum = 1;
            var m = sort % columNum;
            var b = sort / columNum;
            return new Point(m * examRoomWidth, b * examRoomHeight);
        }

        private void ClearTextBox()
        {

            txtIdCard.Text = "";
            lbGuid.Text = "";
            lbCXJG.Text = "";
            lbKSZG.Text = "";
            lbKSJB.Text = "";
            lbXM.Text = "";
            lbSFZH.Text = "";
            lbKSH.Text = "";
            lbZKZH.Text = "";
            lbSYD.Text = "";
            lbLXDH.Text = "";
            lbYYZB.Text = "";
            lbYYSJ.Text = "";
            lbJFZT.Text = "";
            pictureBox1.Image = null;
            lbSKID.Text = "";
        }
        #region 身份证输入框回车事件
        private void txtIdCard_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (CheckIDCard.CheckTextEditIdCardNo(txtIdCard, e))
            {
                CheckAppointmentStudents(txtIdCard.Text.Trim());
            }
        }
        string SignUpID = string.Empty;
        private void CheckAppointmentStudents(string strIdCard)
        {
               DataTable dt = service.CheckStudentAppointmentDataTable_Second(strIdCard, SessionerHelper.ExamPlaceID, SessionerHelper.SpecialityKindsNO, SessionerHelper.ExamLevel);
            if (dt == null || dt.Rows.Count < 1)
            {
                DevHelper.ShowErrorMessageBox("无该考生报名信息！");
                return;
            }
            else if (dt.Rows.Count > 1)
            {
                //AppointmentSecond_SpecialityKinds option = new AppointmentSecond_SpecialityKinds(dt, strIdCard);
                //if (option.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                //{
                //    dt = option.dtNewTable;
                //}
                //else
                //{
                //    return;
                //}
            }
            if (dt.Rows[0]["AllowExamID"].ToString().Trim() == "")
            {
                DevHelper.ShowErrorMessageBox("该考生的准考证号为空！");
                return;
            }
            SignUpID = dt.Rows[0]["SignUpID"].ToSafeString();
            lbGuid.Text = dt.Rows[0]["SignUpStudentsSpecialityID"].ToString().Trim();
            lbSKID.Text = dt.Rows[0]["SpecialityKindsNO"].ToString().Trim();
            string strGroupNo = GetGourpIDBySK(lbSKID.Text);
            lbCXJG.Text = "查询成功。";
            lbKSZG.Text = dt.Rows[0]["IsActive"].ToString().Equals("1") ? "有效" : "无效";
            //using (DataTable dtExamLevel = bllcommService.GetExamLevelTable())
            //{
            //    DataRow[] drs = dtExamLevel.Select("DicNO='" + SessionerHelper.ExamLevel + "'");
            //    if (drs.Length > 0)
            //    {
            //        lbKSJB.Text = drs[0]["DicName"].ToSafeString();
            //    }
            //}
            lbXM.Text = dt.Rows[0]["Name"].ToString().Trim();
            lbSFZH.Text = dt.Rows[0]["IdentityCard"].ToString().Trim();
            lbKSH.Text = dt.Rows[0]["NMT_SignUpID"].ToString().Trim();
            lbZKZH.Text = dt.Rows[0]["AllowExamID"].ToString().Trim();
            using (DataTable dtExamLevel = bllcommService.GetDicDataTableByDicTableName("TB_Dic_ExamProvince"))
            {
                DataRow[] drs = dtExamLevel.Select("DicNO='" + dt.Rows[0]["ExamProvince"].ToString().Trim() + "'");
                if (drs.Length > 0)
                {
                    lbSYD.Text = drs[0]["DicName"].ToSafeString();
                }
            }
            lbLXDH.Text = dt.Rows[0]["LinkMobile"].ToString().Trim();
            //var modelGroup = service.GetAppointmentGroupModel_Second(dt.Rows[0]["AppointmentGroup"] != null ? dt.Rows[0]["AppointmentGroup"].ToString() : "");
            //if (modelGroup != null)
            //{
            //    lbYYZB.Text = modelGroup.GroupName;
            //}
            lbYYSJ.Text = dt.Rows[0]["AppointmentGroupTime"] != null ? dt.Rows[0]["AppointmentGroupTime"].ToString() : "";
            lbJFZT.Text = ConstAppConfig.GetIsFee(dt.Rows[0]["IsFee"].ToString());

            if (!ConstAppConfig.YJF_Value.Equals(dt.Rows[0]["IsFee"].ToString()))
            {
                DevHelper.ShowInfoMessageBox("该考生" + ConstAppConfig.WJF + "！");
                return;
            }
            if (!string.IsNullOrEmpty(lbYYZB.Text))
            {
                XtraMessageBox.Show("该考生已经预约考试时间，信息如下：预约组-》" + lbYYZB.Text + "，预约时间-》" + lbYYSJ.Text + "。如需重新分配请先删除之前信息");
                return;
            }
            lbYYZB.Text = this.GetGourpIDBySK(this.lbSKID.Text);
            if (string.IsNullOrEmpty(lbYYZB.Text))
            {
                XtraMessageBox.Show("该考点专业级别还没有设置分组信息，请先进行分组再操作！");
                return;
            }

            if (XtraMessageBox.Show("该考生" + lbJFZT.Text + ",是否预约？", "确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                lbYYSJ.Text = DateTime.Now.ToString();
                var result = service.MakeAppointment_Second(lbGuid.Text, lbYYZB.Text);
                SaveIDCardInfo(SignUpID);

                if (this.xtraScrollableControl1.Controls != null)
                {
                    Control.ControlCollection controlColl = this.xtraScrollableControl1.Controls;
                    int listCount = controlColl.Count;
                    for (int c = 0; c < listCount; c++)
                    {
                        //ExamSpecialityKinds examRoom = (ExamSpecialityKinds)controlColl[c];
                        //if (lbYYZB.Text == examRoom.GroupNO)
                        //{
                        //    examRoom.InitAppointmentStudents();
                        //}
                    }
                }
            }
            ClearTextBox();
        }
        private void SaveIDCardInfo(string strSignUpID)
        {
            try
            {
                Image image = pictureBox1.Image;
                if (image != null && !string.IsNullOrEmpty(strSignUpID))
                {
                    //string PicDataD = ImageConvertHelper.ToBase64String(pictureBox1.Image, System.Drawing.Imaging.ImageFormat.Bmp);
                  //service.SaveAppointmentIdCard_Second(strSignUpID, PicDataD);
                }
            }
            catch
            { }
        }
        #endregion
        private string GetGourpIDBySK(string specialityKindsNo)
        {
            //using (DataTable dtGroup = new RecruitStudents.CommData.Service.ServiceControlDataSource().GetAppointmentGroupDataTable(SessionerHelper.ExamPlaceID, specialityKindsNo, SessionerHelper.ExamLevel))
            //{
            //    if (dtGroup != null && dtGroup.Rows.Count > 0)
            //    {
            //        return dtGroup.Rows[0]["GroupNo"].ToSafeString();
            //    }
            return string.Empty; 
            //}
        }

        private void Appointment_MakeAppointmentForm_Second_List_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.timer_ReadID.Enabled = false;
        }
    }
}
